package com.jic.member.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberLabel;
import com.jic.member.request.MememberLabelRequest;
import com.jic.member.response.MememberLabelResponse;
import com.jic.member.service.MememberLabelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/memberLabel")
public class MemberLabelController {

    @Resource
    MememberLabelService mememberLabelService;
    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取会员标签关联所有列表")
    @ResponseBody
    @RequestMapping("/queryMememberLabelList")
    public RestResult<PageResult<MememberLabelResponse>> queryMememberLabelList(@RequestBody MememberLabelRequest form) {
        log.info("开始进入会员标签列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberLabel memberRequest = BeanUtil.copy(form, MememberLabel.class);
        PageResult<MememberLabel> result = mememberLabelService.queryPage(memberRequest, page);
        List<MememberLabelResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MememberLabelResponse memberResponse = BeanUtil.copy(temp, MememberLabelResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MememberLabelResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "保存会员标签信息")
    @ResponseBody
    @RequestMapping("/saveMememberLabelInfo")
    public RestResult saveMememberLabelInfo(@RequestBody MememberLabelRequest form){
        MememberLabel memberRequest = BeanUtil.copy(form, MememberLabel.class);
        return RestResult.success(mememberLabelService.insert(memberRequest));
    }

    @ApiOperation(value = "删除会员标签信息")
    @ResponseBody
    @RequestMapping("/deleteMememberLabelInfo")
    public RestResult deleteMememberLabelInfo(@RequestBody MememberLabelRequest form){
        return RestResult.success(mememberLabelService.delete(form.getId()));
    }
}
